package com.quanjiaolian.userinfo.serviceimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.quanjiaolian.DB.DataBase;
import com.quanjiaolian.userinfo.service.MsgCenterService;

public class MsgCenterServiceImpl implements MsgCenterService {

	DataBase db = new DataBase();
	
	 /**
    * 声明数据库连接实例
    */
	//定义一个Connection 用来连接数据库 
	Connection conn=null;  
   //声明PreparedStatement实例
   PreparedStatement pstmt = null;
   //用于存放返回结果的数据集
   ResultSet rs = null;
	
	@SuppressWarnings("static-access")
	public List<Object> queryUserMsgById(int replay_id,int user_type) {
		StringBuffer sql = new StringBuffer();
		List<Object> list = new ArrayList<Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(user_type == 0){
			sql.append("SELECT ri.chat_id,ui.username,ui.userpic_path,ri.review_time,ri.re_id ");
			sql.append("FROM review_info ri,user_info ui ");
			sql.append("WHERE ui.userid = ri.review_id AND ri.replay_id = ? AND ri.review_type = 0");
		}else{
			sql.append("SELECT ri.chat_id,ii.ins_name,ii.inspic_path,ri.review_time,ri.re_id ");
			sql.append("FROM review_info ri,ins_info ii ");
			sql.append("WHERE ri.review_id = ii.ins_id AND ri.replay_id = ? AND ri.review_type = 0");
		}
		conn = db.getconn();
		try {
			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setInt(1,replay_id);
			rs = pstmt.executeQuery();
			while(rs.next()){
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("chat_id",rs.getInt(1));
				map.put("user_name",rs.getString(2));
				map.put("userpic_path",rs.getString(3));
				map.put("review_time",sdf.format(rs.getTimestamp(4)));
				map.put("re_id",rs.getInt(5));
				list.add(map);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			db.close(conn, pstmt, rs);
		}
		
		return list;
	}
	
	@SuppressWarnings("static-access")
	public List<Object> queryAdminMsg(){
		List<Object> list = new ArrayList<Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		StringBuffer sql = new StringBuffer();
		sql.append("select mp.chat_id,mp.push_date from mag_push mp ");
		sql.append("WHERE mp.push_success = 0");
	
		conn = db.getconn();
		try {
			pstmt = conn.prepareStatement(sql.toString());
			rs = pstmt.executeQuery();
			while(rs.next()){
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("chat_id",rs.getInt(1));
				map.put("user_name","全教练运营团队");
				map.put("userpic_path","/upload/adminPic/icon.png");
				map.put("send_time",sdf.format(rs.getTimestamp(2)));
				list.add(map);
			}
		} catch (SQLException e) {
			System.out.println("查询出错！");
			e.printStackTrace();
		}finally{
			db.close(conn, pstmt, rs);
		}
		
		return list;
	}
	
	@SuppressWarnings("static-access")
	public int updateMsgState(int re_id){
		int i = 0;
		String sql = "update review_info set review_type=1 where re_id = ?";
		conn = db.getconn();
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1,re_id);
			i = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.close(conn, pstmt, rs);
		}
		return i;
	}

}
